package com.yingda.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

import com.hjq.toast.ToastUtils;

import java.io.File;
import java.io.IOException;

public class FileUtils {


    public static boolean createOrExistsFile(File fileDir) {

        return fileDir.exists();
    }


    public static boolean createOrExistsFile(Context context) {
        File file1;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            // 设备运行的是 Android 10 或更高版本
            file1 = new File(context.getExternalFilesDir(null), "MyFile");
        } else {
            // 设备运行的是 Android 9 或更低版本
            File fileDir = Environment.getExternalStorageDirectory();
             file1 = new File(fileDir,"MyFile");
        }
        boolean mkdirs = false;
        if (!file1.exists()){
             mkdirs = file1.mkdirs();
        }else {
            mkdirs= true;
        }
        return mkdirs;
    }

    //获取表格1 的路径
    public static String getTab1Path() {
        File fileDir = Environment.getExternalStorageDirectory();
        String path = fileDir.getAbsoluteFile() + File.separator +"MyFile";
        File file = new File(path,"表格1.xls");
       if (file.exists()){
           return file.getAbsolutePath();
       }
        return "";

    }


    //获取表格1 的路径
    public static String getTab2Path() {
        File fileDir = Environment.getExternalStorageDirectory();
        String path = fileDir.getAbsoluteFile() + File.separator +"MyFile";
        File file = new File(path,"表格2.xlsx");
        if (file.exists()){
            return file.getAbsolutePath();
        }
        return "";

    }


    public static void openDownloadFolder(Context context){
        File downloadFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        if (downloadFolder.exists()) {
            Uri uri = Uri.parse(downloadFolder.toString());
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "*/*");  // 显示所有文件类型
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  // 授予读取权限

            // 检查系统中是否有合适的应用可以处理此 Intent
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            } else {
                ToastUtils.show("没有找到合适的应用来打开下载文件夹");

            }
        } else {
            ToastUtils.show("下载文件夹不存在");

        }
    }
}
